#include "movebycursorwindow.h"

#include <QVBoxLayout>
#include <QPushButton>
#include <QDebug>
#include <QIcon>
#include <QTimer>

MoveByCursorWindow::MoveByCursorWindow(QWidget *parent)
    : QWidget(parent)
    , m_w(nullptr)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    setLayout(mainLayout);

    QPushButton *openBtn = new QPushButton("打开待移动窗口", this);
    mainLayout->addWidget(openBtn);

    connect(openBtn, &QPushButton::clicked, this, [this] {
        if (!m_w) {
            m_w = new DMainWindow;
            m_w->setWindowFlags(windowType() | Qt::WindowType::WindowStaysOnTopHint);
            m_w->setFocus(Qt::FocusReason::MouseFocusReason);
            m_w->setFocus();
        }
        m_w->show();
        m_w->activateWindow();
    });
}

MoveByCursorWindow::~MoveByCursorWindow()
{
}

#include <QMouseEvent>

void MoveByCursorWindow::mousePressEvent(QMouseEvent *event)
{
    if (Qt::MouseButton::LeftButton == event->button()) {
        if (!m_w) {
            return;
        }

        m_w->move(QCursor::pos());
    }
}
